package com.glorysoft.xztpda.ui.orcomment

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityOrCommentBinding
import com.glorysoft.xztpda.utils.PopWindowsUtils
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils
import kotlinx.android.synthetic.main.activity_or_comment.*
import kotlinx.android.synthetic.main.activity_or_comment.flScrap
import kotlinx.android.synthetic.main.activity_or_comment.ivScrap
import kotlinx.android.synthetic.main.activity_or_comment.tvScrap

/**
 * FileName: OrCommentActivity
 * Author: XJH
 * Date: 2022/4/20 11:44
 * Description:
 * History:
 */
class OrCommentActivity : BaseActivity<OrCommentViewModel, ActivityOrCommentBinding>() {
    private var codePop: PopWindowsUtils.GeneralPop? = null
    override fun layoutId(): Int {
        return R.layout.activity_or_comment
    }

    override fun initVariableId(): Int {
        return BR.orCommentViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("OR异常单登记")
        setRightListener("提交") {
            if (viewModel.orData == null) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到OR信息", false)
                return@setRightListener
            }

            viewModel.submit(
                etOrNum.text.toString(),
                etComment.text.toString().replace("\\s".toRegex(), ""),
                etCor.text.toString().replace("\\s".toRegex(), ""),
                tvScrap.text.toString(),
                etRespte.text.toString(),
                etRespteAder.text.toString()
            )
        }

        ivOrScan.setOnClickListener { startScanForCode(this, 10001) }
        flScrap.setOnClickListener {
            if (codePop == null) {
                codePop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flScrap.width,
                    viewModel.bilityList,
                    ivScrap,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        override fun onSuccess(string: String, potion: Int) {
                            tvScrap.text = string
                            llRes.visibility = if (string == "人") View.VISIBLE else View.GONE

                        }
                    })

            }
            if (codePop!!.isShowing) {
                codePop?.dismiss()
            } else {
                FreeApi_PopUtils.setArrowShow(ivScrap)
                codePop?.showAsDropDown(flScrap, 0, 0)
            }
        }
    }

    override fun initData() {
        initEdit(etOrNum, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.getData(s)
            }

        })

        viewModel.defUI.callObserve.observe(this) {
            mBinding.setVariable(BR.orCommentViewModel, viewModel)
        }

        viewModel.defUI.startActivity.observe(this) {
            viewModel.defUI.toastEvent.value = ToastMessageBean("操作成功", true)
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK) {
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when (requestCode) {
                        10001 -> {
                            etOrNum.setText(barCode)
                            viewModel.getData(barCode)
                        }

                    }

                }
            }
        }
    }
}